/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ru.ssau.entities;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import javax.persistence.*;
import org.jdom.Element;
import ru.ssau.lib.PresentableAsStringIntegerPair;
import ru.ssau.lib.StringIntegerPair;
import ru.ssau.lib.XMLizable;
import ru.ssau.utils.JMSUtil;
import ru.ssau.utils.XMLHelper;


@Entity
@Table(name="Users")
@NamedQueries({
    @NamedQuery(name=User.FIND_ALL_USERS, query="SELECT u FROM User u"),
    @NamedQuery(name=User.FIND_USERS_BY_NAME, query="SELECT u FROM User u WHERE UPPER(u.name) LIKE ?1")})
public class User implements Serializable, PresentableAsStringIntegerPair, XMLizable {

    @Transient
    public static final String FIND_ALL_USERS = "findAllUsers";
    @Transient
    public static final String FIND_USERS_BY_NAME = "findUsersByName";

    @Id
    @Column(name="UserID", nullable=false)
    @SequenceGenerator(name="UserIdGenerator", sequenceName="UserIDSeq", allocationSize=1)
    @GeneratedValue(generator="UserIdGenerator")
    private Integer id;

    @Column(name="UserName", nullable=false, length=50)
    private String name;

    @Column(name="RegDate", nullable=false)
    @Temporal(TemporalType.DATE)
    private Date registrationDate;

    @Column(name="UserInfo")
    private String info;

    @OneToOne(cascade={CascadeType.PERSIST, CascadeType.REMOVE}, mappedBy = "user")
    private AuthorizationInfo authorizationInfo;

    public User() {

    }

    public User(String name, Date regDate, String info, AuthorizationInfo authInfo) {
        this.name = name;
        this.registrationDate = regDate;
        this.info = info;
        this.authorizationInfo = authInfo;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getRegistrationDate() {
        return registrationDate;
    }

    public void setRegistrationDate(Date registrationDate) {
        this.registrationDate = registrationDate;
    }

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }

    public AuthorizationInfo getAuthorizationInfo() {
        return authorizationInfo;
    }

    public void setAuthorizationInfo(AuthorizationInfo authorizationInfo) {
        this.authorizationInfo = authorizationInfo;
    }

    @PostPersist
    private void sendUserCreatedLogMessage() {
        JMSUtil.sendLogMessage(JMSUtil.USER, id, new Date(), JMSUtil.ADD);
    }

    @PostRemove
    private void sendUserRemovedLogMessage() {
        JMSUtil.sendLogMessage(JMSUtil.USER, id, new Date(), JMSUtil.REMOVE);
    }

    @PostUpdate
    private void sendUserUpdatedLogMessage() {
        JMSUtil.sendLogMessage(JMSUtil.USER, id, new Date(), JMSUtil.UPDATE);
    }

    @Override
    public boolean equals(Object obj) {
        if(obj == null) {
            return false;
        }
        if(obj == this) {
            return true;
        }
        if(!(obj instanceof ru.ssau.entities.Thread)) {
            return false;
        }
        User other = (User) obj;

        return id.equals(other.id) && name.equals(other.name) &&
               info.equals(other.info) &&
               registrationDate.equals(other.registrationDate) &&
               authorizationInfo.equals(other.authorizationInfo);
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 59 * hash + (this.id != null ? this.id.hashCode() : 0);
        hash = 59 * hash + (this.name != null ? this.name.hashCode() : 0);
        hash = 59 * hash + (this.registrationDate != null ? this.registrationDate.hashCode() : 0);
        hash = 59 * hash + (this.info != null ? this.info.hashCode() : 0);
        hash = 59 * hash + (this.authorizationInfo != null ? this.authorizationInfo.hashCode() : 0);
        return hash;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(getClass().getName());
        sb.append(": [id=");
        sb.append(id);
        sb.append(", name=");
        sb.append(name);
        sb.append(", regDate=[");
        sb.append(registrationDate.toString());
        sb.append("], info=");
        sb.append(info);
        sb.append(", authInfo=[");
        sb.append(authorizationInfo.toString());
        sb.append("]]");
        return sb.toString();
    }

    public Element toXMLElement() {
        Element root = new Element("User");
        XMLHelper.addElement(root, "id", id);
        XMLHelper.addElement(root, "name", name);
        XMLHelper.addElement(root, "registrationDate", registrationDate);
        XMLHelper.addElement(root, "info", info);
        XMLHelper.addElement(root, authorizationInfo.toXMLElement());
        return root;
    }
    
    public void loadFromXMLElement(Element elmnt) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public StringIntegerPair toStringIntegerPair() {
        return new StringIntegerPair(id, name);
    }

}
